namespace Brjnk.GpxExplorer.Workspace.BitmapExport
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Brjnk.Common;
    using Brjnk.GpxExplorer.UI;

    /// <summary>
    /// </summary>
    public class BitmapExporter
    {
        private AreaManager area;

        private BitmapExportEditor editor;

        private DataModel data;

        private DataView view;

        public BitmapExporter(AreaManager area, DataView view, DataModel data)
        {
            this.area = area;
            this.data = data;
            this.view = view;
            CreateCommands();
        }

        private void Export(Action<RenderTargetBitmap> resultHandler)
        {
            if (editor != null && editor.IsCompleted == false) return;

            var paperPicker = new BitmapExportPaperWindow();
            if (paperPicker.ShowDialog() ?? false)
            {
                if (!paperPicker.UseActualSize)
                {
                    var dip = paperPicker.SelectedPaper.ToDip();
                    area.SetSize(dip);
                }
            }
            else { return; }

            editor = new BitmapExportEditor(area, view, data, o => { editor.Destroy(); resultHandler(o); });
            editor.EndAction = new Action(area.ResetSize);
            editor.Show();
        }

        private Visual GetWhiteBackground()
        {
            DrawingVisual visual = new DrawingVisual();
            using (var context = visual.RenderOpen())
            {
                context.DrawRectangle(Brushes.White, null, new Rect(0, 0, area.Area.ActualWidth, area.Area.ActualHeight));
            }
            return visual;
        }

        private void ExportBitmapToPng(RenderTargetBitmap bitmap)
        {
            var sfd = new Microsoft.Win32.SaveFileDialog();
            if ((sfd.ShowDialog() ?? false) == true)
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                using (var stream = new System.IO.FileStream(sfd.FileName, System.IO.FileMode.Create))
                {
                    encoder.Save(stream);
                }
            }
        }

        private void ExportBitmapToClipboard(RenderTargetBitmap bitmap)
        {
            System.Windows.Clipboard.SetImage(bitmap);
        }

        private void CreateCommands()
        {
            exportBitmapCommand = new GenericCommand(o => Export(r => ExportBitmapToPng(r)));
            exportBitmapToClipboardCommand = new GenericCommand(o => Export(r => ExportBitmapToClipboard(r)));
        }

        private GenericCommand exportBitmapCommand;

        public GenericCommand ExportBitmapCommand { get { return exportBitmapCommand; } }

        private GenericCommand exportBitmapToClipboardCommand;

        public GenericCommand ExportBitmapToClipboardCommand { get { return exportBitmapToClipboardCommand; } }
    }
}